package com.beiding.listener;

import com.beiding.service.MessageService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AmqpMessageListener implements MessageListener {

    private MessageService messageService;

    @Autowired
    public void setMessageService(MessageService messageService) {
        this.messageService = messageService;
    }

    private MessageConverter converter;

    @Autowired
    public void setConverter(MessageConverter converter) {
        this.converter = converter;
    }

    @Override
    public void onMessage(Message message) {

        try {
            com.beiding.pojo.Message m = (com.beiding.pojo.Message) converter.fromMessage(message);
            if (m != null)
                messageService.send(m.getReceiver(), m.getTopic(), m.getPayload());
        } catch (Exception ignore) {
        }


    }


}
